package com.manager.common.customer.utils;

import com.manager.common.utils.DataTools;
import com.manager.common.vo.ResultVO;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import okhttp3.MediaType;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;

import java.util.List;
import java.util.concurrent.TimeUnit;

public class RemoteApi {

    public static ResultVO executeHttp(String url) throws Exception{
        return executeHttp(url,new JSONObject());
    }
    public static ResultVO executeHttp(String url,JSONObject params) throws Exception{
        OkHttpClient client = new OkHttpClient.Builder()
                .connectTimeout(30, TimeUnit.SECONDS)
                .callTimeout(120, TimeUnit.SECONDS)
                .pingInterval(5, TimeUnit.SECONDS)
                .readTimeout(60, TimeUnit.SECONDS)
                .writeTimeout(60, TimeUnit.SECONDS)
                .build();
        Request request = new Request.Builder().url(url)
                .addHeader("User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:79.0) Gecko/20100101 Firefox/79.0")
                .post(okhttp3.RequestBody.create(MediaType.parse("application/json; charset=utf-8"), params.toString()))
                .build();
        Response response = client.newCall(request).execute();

        String str = response.body().string();
        JSONObject jsonObject = JSONObject.fromObject(str);

        ResultVO resultVO = new ResultVO();
        resultVO.setCode(jsonObject.getInt("code"));
        resultVO.setMsg(jsonObject.getString("msg"));
        resultVO.setData(jsonObject.get("data"));

        return  resultVO;
    }
}
